<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.Map"%>
<%@ page import="com.itic.appbase.framework.utils.RSAHelper"%>
<%@ page import="java.security.interfaces.RSAPrivateKey"%>
<%@ page import="java.security.interfaces.RSAPublicKey"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <link href="<%=request.getContextPath()%>/static/styles/css/reset.css" rel="stylesheet">
    <link href="<%=request.getContextPath()%>/static/styles/css/login.css" rel="stylesheet">
    <title>登陆界面</title>
</head>
<body>

<div class="login-container">
<div class="login-header"></div>
<div class="login-body-bg">
<div class="login-body-bg-right"></div>
	<div class="login-body">
		<div class="login-form">
			
			<input type="hidden" id="portal" name="portal" value=""/>
			<div class="tip-wrap">
				<div id="tip"></div>
			</div>
			<p>
				<span class="label user-label"></span>
				<input type="text" id="username" name="username" placeholder="用户名"/>
			</p>
			<p>
				<span class="label password-label"></span>
				<input type="password" id="password" name="password" placeholder="密码"/>
			</p>
			<p>
				<input type="text" id="captcha" name="captcha"/>
       			<img id="validateCode" src="servlet/validateCodeServlet?height=34&width=80" onclick="$('#validateCode').attr('src','servlet/validateCodeServlet?height=34&width=80&'+new Date().getTime())"/>
			</p>
			<p>
				<a id="loginBtn" class="login-btn" onclick="login();" href="javascript:;">登录</a>
			</p>
		</div>
	</div>
	
</div>
</div>

<!-- <div id="logo"></div>
<div id="loginDiv">
	<div id="tip"></div>
   <p style="color:#fff;font-size: 16px; padding-bottom: 10px; border-bottom: 1px solid #bbb;">用户登录</p>
   <p>
       <input type="text" id="username" name="username" placeholder="用户名"/>
       程序入口
       <input type="hidden" id="portal" name="portal" value=""/>
   </p>
   <p>
       <input type="password" id="password" name="password" placeholder="密码"/>
   </p>
   <p id="code">
       <input type="text" id="captcha" name="captcha"/>
       <img id="validateCode" src="servlet/validateCodeServlet" onclick="$('#validateCode').attr('src','servlet/validateCodeServlet?'+new Date().getTime())"/>
   </p>
   <p>
       <input type="button" value="登录" id="loginBtn" onclick="login();"/>
   </p>
</div> -->
<script src="<%=request.getContextPath()%>/static/js/jquery-1.9.1.min.js"></script>
<script src="<%=request.getContextPath()%>/static/js/security.js"></script>
<%
		// Generate key Pair
		Map<String, Object> map = RSAHelper.getKeys();  
		RSAPublicKey publicKey = (RSAPublicKey) map.get("public");  
		RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
		// Put private key into session
		session.setAttribute("privateKey", privateKey);
		// hold public key for javascript
		String publicKeyExponent = publicKey.getPublicExponent().toString(16);
		String publicKeyModulus = publicKey.getModulus().toString(16);
		request.setAttribute("publicKeyExponent", publicKeyExponent);
		request.setAttribute("publicKeyModulus", publicKeyModulus);
	%>
<script>
	var basePath = '<%=request.getContextPath()%>',
		path = basePath + '/login';
	$(function() {
		/* 回车登录*/
		$("#loginDiv input").keydown(function(event) {
			if (event.keyCode == 13) {
				login();
			}
		});
	});
	/* 登录  */
	function login() {
		var loginName = $("#username").val(),
		    password = $("#password").val(),
		    captcha = $("#captcha").val(),
		    portal = $('#portal').val();
		
		if (!loginName || !password) {
			Tip.show('#tip', 'empty_param');
            return false;
		}
		
		if (!captcha) {
            Tip.show('#tip', 'empty_captcha');
            return false;
        }
		
		// set reasonable size 
        RSAUtils.setMaxDigits(200);
        // compute public key
        var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "",
                "${publicKeyModulus}");
        // encrypt password
        var encrypedPwd = RSAUtils.encryptedString(key, password);
        
        $.ajax({
            type: 'POST',
            url : path,
            data: {'username': loginName, 'password' : encrypedPwd, 'captcha' : captcha, 'portal' : portal},
            dataType: 'json',
            success: function (data) {
                var result = data.result;
                if (result == 'success') {
                    location.href = basePath + '/main';
                } else {
                    Tip.show('#tip', result);
                }
            }
        });
	}
	<%-- function login() {
        var password = $("#password").val();
        var username = $("#username").val();
        // set reasonable size 
        RSAUtils.setMaxDigits(200);
        // compute public key
        var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "",
                "${publicKeyModulus}");
        // encrypt password
        var encrypedPwd = RSAUtils.encryptedString(key, password);
        // post
        $.post("<%=request.getContextPath()%>/login", {
            loginName: username,
            loginPwd : encrypedPwd
        }, function(jsonData) {
                alert(jsonData);
        });
    } --%>
	var Tip = {
            msg: {
                empty_param : '用户名或密码不能为空',
                empty_captcha: '请输入验证码',
                credentials_invalid : '用户名或密码不正确',
                captcha_invalid: '验证码错误',
                server_error: '服务器错误',
                pwd_empty:    '密码不能为空',
                repeat_pwd_error: '两次输入的密码不一致',
                cur_pwd_error:    '当前密码输入不正确',
                unkown_error: '服务器繁忙,请稍后重试',
                length_pwd_error: '密码长度必须为6-16位',
                no_user: '没有此用户',
                user_disabled: '用户被禁用'
            },      
            getMsg: function (code) {
                return Tip.msg[code] || Tip.msg['unkown_error'];
            },
            show: function (ele, code) {
                $(ele).text(Tip.getMsg(code)).fadeIn('slow');
                Tip.hide(ele);
            },
            hide: function (ele) {
            	window.setTimeout(function(){
            		$(ele).fadeOut(1000);
            	},2000);
            }
    };
</script>	
</body>
</html>
